/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class code1 {
    //先走差值步
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int len1 = 0,len2 = 0;
        ListNode cur1 = headA,cur2 = headB;
        while(cur1 != null) {
            len1++;
            cur1 = cur1.next;
        }
        while(cur2 != null) {
            len2++;
            cur2 = cur2.next;
        }
        int len = len1>len2 ? len1-len2:len2-len1;
        if(len1 > len2) {
            while(len-- > 0) {
                headA = headA.next;
            }
        } else {
            while(len-- > 0) {
                headB = headB.next;
            }
        }
        while(headA != null && headB != null) {
            if(headA == headB) {
                return headA;
            } else {
                headA = headA.next;
                headB = headB.next;
            }
        }
        return null;
    }
}